package edu.unc.comp410.colemangeer.model.constraints;


public class CompoundZipCodeConstraint extends Constraint {

	public CompoundZipCodeConstraint(String theName, IntegerConstraint theIntegerConstraint,
			ZipCodeStringConstraint theStringConstraint){
		super(theName);
		this.integerConstraint = theIntegerConstraint;
		this.stringConstraint = theStringConstraint;
	}
	
	@Override
	public boolean isLegal(Object obj) {
		
		String stringValue;
		Integer integerValue;
		
		try {
			stringValue = (String) obj;
		} catch (ClassCastException e) {
			return false;
		}
		
		try {
			integerValue = Integer.parseInt(stringValue);
		} catch (NumberFormatException e) {
			return false;
		}
		
		return stringConstraint.isLegal(stringValue) && integerConstraint.isLegal(integerValue);
	}

	private ZipCodeStringConstraint stringConstraint;
	private IntegerConstraint integerConstraint;
}
